//
//  YZChatCell.m
//  CocoaAsyncSocketClient
//
//  Created by lixiaoyi on 2018/6/20.
//  Copyright © 2018年 lixiaoyi. All rights reserved.
//

#import "YZChatCell.h"
#import "ChatHeader.h"
#import "ImMessage.pbobjc.h"

@implementation YZChatCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // headerImageView
        self.headerImageView = [[UIImageView alloc] init];
        self.headerImageView.layer.cornerRadius = 25.0f;
        self.headerImageView.layer.masksToBounds = YES;
        self.headerImageView.clipsToBounds = YES;
        [self.contentView addSubview:self.headerImageView];
        
        // backImageView
        self.messageBackImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.messageBackImageView];
        
        // messageLabel
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:17];
        [self.messageBackImageView addSubview:self.contentLabel];
        
        // 去掉选择
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

- (void)refreashCell:(ImMessage *)model {
    // msg
    NSString *msg = [[NSString alloc] initWithData:model.payload encoding:NSUTF8StringEncoding];
    
    // 计算文本高度和宽度
    CGSize size = CGSizeMake(200, CGFLOAT_MAX);
    CGRect rect = [msg boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    
    // headerImage
    UIImage *headImage = nil;
    
    // backImage
    UIImage *backImage = nil;
    
    // textOrgin
    CGFloat txtOrigin = 0;
    
    
    // 发送（显示在右边）
    if (![model.sender isEqualToString:@"1"]) {
        self.headerImageView.frame = CGRectMake(kWidth - 60, rect.size.height - 18, 50, 50);
        self.messageBackImageView.frame = CGRectMake(kWidth - 60 - rect.size.width - 20, 10, rect.size.width + 20, rect.size.height + 20);
        headImage = [UIImage imageNamed:@""];
        backImage = [UIImage imageNamed:@"bubbleMine"];
        txtOrigin = 5;
    } else {
        // 接收（显示在左边）
        self.headerImageView.frame = CGRectMake(10, rect.size.height - 18, 50, 50);
        self.messageBackImageView.frame = CGRectMake(60, 10, rect.size.width + 20, rect.size.height + 20);
        headImage = [UIImage imageNamed:@"headSomeone"];
        backImage = [UIImage imageNamed:@"bubbleSomeone"];
        txtOrigin = 10;
    }
    
    backImage = [backImage stretchableImageWithLeftCapWidth:backImage.size.width/2 topCapHeight:backImage.size.height/2];
    [self.headerImageView setImage:headImage];
    [self.messageBackImageView setImage:backImage];
    
    // 文本内容
    self.contentLabel.frame = CGRectMake(txtOrigin, 5, rect.size.width, rect.size.height);
    [self.contentLabel setText:msg];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
